// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.database.commands;

import org.dengues.core.metadata.MetadataHelper;
import org.dengues.core.metadata.MetadataUtils;
import org.dengues.designer.ui.editors.AbstractGEFCommand;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.model.database.DBTable;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.model.database.DiagramChild;
import org.dengues.model.database.EMFDataType;
import org.dengues.model.database.ForeignKeyLabel;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.util.EList;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.requests.ChangeBoundsRequest;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class DiagramChildSetConstraintCommand extends AbstractGEFCommand {

    private final ChangeBoundsRequest request;

    private final DiagramChild table;

    private final Rectangle newBounds;

    private Rectangle oldBounds;

    /**
     * Qiang.Zhang.Adolf@gmail.com DiagramChildSetConstraintCommand constructor comment.
     * 
     * @param request
     * @param model
     * @param newBounds
     */
    public DiagramChildSetConstraintCommand(ChangeBoundsRequest request, DiagramChild model, Rectangle newBounds) {
        if (model == null || request == null || newBounds == null) {
            throw new IllegalArgumentException();
        }
        this.request = request;
        this.newBounds = newBounds;
        this.table = model;
        setLabel(Messages.getString("DBTableSetConstraintCommand.set.table")); //$NON-NLS-1$
    }

    @Override
    public boolean canExecute() {
        Object type = request.getType();
        // make sure the Request is of a type we support:
        boolean b = (RequestConstants.REQ_MOVE.equals(type) || RequestConstants.REQ_MOVE_CHILDREN.equals(type)
                || RequestConstants.REQ_RESIZE.equals(type) || RequestConstants.REQ_RESIZE_CHILDREN.equals(type));
        if (table instanceof DBTable) {
            if (table.getDbDiagram().getEmfType().equals(EMFDataType.MAPPER)) {
                b = RequestConstants.REQ_RESIZE.equals(type) || RequestConstants.REQ_RESIZE_CHILDREN.equals(type);
            }
        }
        return b;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#execute()
     */
    @Override
    public void execute() {
        oldBounds = new Rectangle(table.getX(), table.getY(), table.getWidth(), table.getHeight());
        table.setX(newBounds.x);
        table.setY(newBounds.y);
        table.setWidth(newBounds.width);
        table.setHeight(newBounds.height);
        DatabaseDiagram dbDiagram = table.getDbDiagram();
        if (dbDiagram != null) {
            EList<DiagramChild> dbChildren = dbDiagram.getDbChildren();
            for (DiagramChild diagramChild : dbChildren) {
                if (diagramChild instanceof ForeignKeyLabel) {
                    ForeignKeyLabel key = (ForeignKeyLabel) diagramChild;
                    String pkName = (String) key.getProperties().get(MetadataUtils.FK_NAME);
                    Point cl = MetadataHelper.getColumnLocation(MetadataHelper.getFKByUnqiueName(pkName, dbDiagram));
                    key.setX(cl.x);
                    key.setY(cl.y);
                }
            }
        }
    }

    @Override
    public void undo() {
        table.setX(oldBounds.x);
        table.setY(oldBounds.y);
        table.setHeight(oldBounds.height);
        table.setWidth(oldBounds.width);
    }

}
